<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The News class for table 'news'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adds the definitions or the enumeration of the news type.
 * 		 Adds the 'add' function.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_util.php';

require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
require_once INSTALLDIR . '/classes/PhotoNews.php';
require_once INSTALLDIR . '/classes/MicroNews.php';

// Definitions.
// Column of status reserves.
define('NEWS_NEWSTYPE_MICRONEWS',				1);
define('NEWS_NEWSTYPE_NOTENEWS',				2);
define('NEWS_NEWSTYPE_EVENTNEWS',				3);
define('NEWS_NEWSTYPE_PHOTONEWS',				4);

class News extends Memcached_DataObject
{
	public $__table = 'news';				// table name
	public $id;								// int(11)  primary_key not_null
	public $newstag_id;						// int(11)  foreign_key
	public $newstype;						// tinyint(4)  not_null
	public $status;							// tinyint(4)
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('News', $key, $value); 
	}
	
	/**
	 * Get the explicit type of news.
	 * Object!
	 */
	function getExplicitNews()
	{
		switch ($this->newstype) {
			case NEWS_NEWSTYPE_MICRONEWS: {
				return MicroNews::staticGet('news_id', $this->id);
			}
			case NEWS_NEWSTYPE_NOTENEWS: {
				return NoteNews::staticGet('news_id', $this->id);
			}
			case NEWS_NEWSTYPE_EVENTNEWS: {
				return EventNews::staticGet('news_id', $this->id);
			}
			case NEWS_NEWSTYPE_PHOTONEWS: {
				return PhotoNews::staticGet('news_id', $this->id);
			}
			default: {
				return null;
			}
		}
	}
	
	/**
	 * Get the news type name of the news.
	 */
	function getNewsTypeName()
	{
		switch ($this->newstype) {
			case NEWS_NEWSTYPE_MICRONEWS: {
				return '微新闻';
			}
			case NEWS_NEWSTYPE_NOTENEWS: {
				return '记事新闻';
			}
			case NEWS_NEWSTYPE_EVENTNEWS: {
				return '活动';
			}
			case NEWS_NEWSTYPE_PHOTONEWS: {
				return '相册';
			}
			default: {
				return 'N/A';
			}
		}
	}
	
	/**
	 * Delete a new and its related SubNews.
	 * FIXME: The nested deletion?
	 */
	function delete()
	{
		/*
		$explicitnews = $this->getExplicitNews();
		if ( !empty($explicitnews)) {
			$explicitnews->delete();
		}
		*/
		
		parent::delete();
	}
	
	/**
	 * Get the ids as a list. Use offset and limit to control
	 * the pagination.
	 * 
	 * For all news!
	 */
	static function getAllNewsidsByList($offset = 0,
									 	$limit = 0)
	{
		$news = new News;
		
		$news->selectAdd();
		$news->selectAdd('id');
		$news->orderBy('id DESC');
		
		if ( !is_null($offset)) {
			$news->limit($offset, $limit);
		}
		
		$newsids = array();
		if ($news->find()) {
			while ($news->fetch()) {
				$newsids[] = $news->id;
			}
		}
		
		$news->free();
		$news = NULL;
		
		return $newsids;
	}
	
	/**
	 * Get the sub news objects by the given newsids.
	 * The Wrapper is just the original news.!
	 * ! Remember to distinguish them!
	 * 
	 * @return ArrayWrapper, the wrappered objects.
	 */
	static function getAllNewsByNewsids($newsids)
	{
		$cache = Cache::instance();
		
		if ( !empty($cache)) {
			$news = array();
			foreach ($newsids as $newsid) {
				$n = News::staticGet('id', $newsid);
				if ( !empty($n)) {
					$news[] = $n;
				}
			}
			
			return new ArrayWrapper($news);
		} else {
			// Miss the cache.
			$news = new News();
			if (empty($newsids)) {
				return $news;
			}
			
			$news->whereAdd('id in (' . implode(', ', $newsids) . ')');
			$news->find();
			
			$temp = array();
			while ($news->fetch()) {
				$temp[$news->id] = clone($news);
			}
			
			$wrapped = array();
			
			foreach($newsids as $newsid) {
				if (array_key_exists($newsid, $temp)) {
					$wrapped[] = $temp[$newsid];
				}
			}
			
			return new ArrayWrapper($wrapped);
		}
	}
}
?>